package com.lancoo.klgcourseware.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.text.Html;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lancoo.klgcourseware.R;
import com.lancoo.klgcourseware.entity.bean.SentenceLineTrainBean;
import com.lancoo.klgcourseware.entity.bean.SentenceTrainBean;
import com.lancoo.klgcourseware.ui.widget.UsageReplaceSpan;

import java.util.List;


/**
 * Created by pengmi on 2018/11/14.
 * Description:用法试题展示适配器
 */

public class SentenceTrainAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<SentenceLineTrainBean> sentenceLineTrainBeanList;
    private int trainTimes=0;
    private int showCount=-1;
    private boolean isQuality;

    public SentenceTrainAdapter(Context mContex, List<SentenceLineTrainBean> sentenceLineTrainBeanList) {
        this.mContext = mContex;
        this.sentenceLineTrainBeanList = sentenceLineTrainBeanList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.klg_list_item_usage_train_sentence, parent, false);
        ContentVh contentVh=new ContentVh(inflate);
        return contentVh;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ContentVh contentVh= (ContentVh) holder;
        contentVh.initData(sentenceLineTrainBeanList.get(position));
    }

    @Override
    public int getItemCount() {
        return showCount>0?showCount:(sentenceLineTrainBeanList ==null?0:sentenceLineTrainBeanList.size());
    }

    private class ContentVh extends RecyclerView.ViewHolder{

        protected TextView tv_eContent;
        protected TextView tv_cContent;
//        protected TextPaint paint;

        public ContentVh(View itemView) {
            super(itemView);
            initView(itemView);
        }

        private void initView(View view){
            tv_eContent = view.findViewById(R.id.tv_english_content);
            tv_cContent = view.findViewById(R.id.tv_chinese_content);
//            paint = new TextPaint(tv_eContent.getPaint());
        }

        public void initData(SentenceLineTrainBean wordPracticeModel){
            tv_cContent.setText(Html.fromHtml(wordPracticeModel.getChineseSen()));
            SpannableStringBuilder spannableStringBuilder=new SpannableStringBuilder();
            List<SentenceTrainBean> sentenceTrainBeanList = wordPracticeModel.getSentenceTrainBeanList();
            for (int i = 0; i <sentenceTrainBeanList.size() ; i++) {
                SentenceTrainBean sentenceTrainBean = sentenceTrainBeanList.get(i);
                if(sentenceTrainBean.isFill()){//当前分句是否填空处
                    String sentence = sentenceTrainBean.getSentence();
                    if(trainTimes==3){
                        UsageReplaceSpan usageReplaceSpan=new UsageReplaceSpan(mContext,new TextPaint(tv_eContent.getPaint()));
                        String content="×";
                        SpannableString spannableString=new SpannableString("0 ");
                        usageReplaceSpan.setWordName(content);
                        usageReplaceSpan.setmText(content);
                        usageReplaceSpan.setDrawTextColor(mContext.getResources().getColor(R.color.klg_unquality_color));
                        spannableString.setSpan(usageReplaceSpan,0,1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                        spannableStringBuilder.append(spannableString);
                    }else if(trainTimes==0){
                        UsageReplaceSpan usageReplaceSpan=new UsageReplaceSpan(mContext,new TextPaint(tv_eContent.getPaint()));
                        String content="("+(sentenceTrainBean.getFillIndex())+")";
                        SpannableString spannableString=new SpannableString("0 ");
                        usageReplaceSpan.setWordName(content);
                        usageReplaceSpan.setmText(content);
                        usageReplaceSpan.setDrawTextColor(Color.BLACK);
                        spannableString.setSpan(usageReplaceSpan,0,1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                        spannableStringBuilder.append(spannableString);
                    }else{
                        String[] subSplit = sentence.split(" ");
                        int index=0;
                        for (int k = 0; k <subSplit.length ; k++) {
                            if(TextUtils.isEmpty(subSplit[k].trim())){
                                spannableStringBuilder.append(" ");
                                continue;
                            }
//                            WordTrainBean wordTrainBean = sentenceTrainBean.getWordTrainBeanList().get(index);
                            String content=(k==0?"  ":"")+""+subSplit[k]+(k==subSplit.length-1?"   ":"");
                            SpannableString spannableString=new SpannableString("0 ");
                            UsageReplaceSpan subUsageReplaceSpan=new UsageReplaceSpan(mContext,new TextPaint(tv_eContent.getPaint()));
                            subUsageReplaceSpan.setWordName(content);
                            subUsageReplaceSpan.setSubText(content);
                            Log.e("aaaaaaaaaaaa","trainTimes:"+trainTimes);
                            if(trainTimes==1){
//                                subUsageReplaceSpan.setDrawTextColor(mContext.getResources().getColor(wordTrainBean.getScore()< KlgConstant.SCORE_QUALITY ?R.color.klg_unquality_color:R.color.klg_quality_color));
                                subUsageReplaceSpan.setDrawTextColor(mContext.getResources().getColor(R.color.klg_quality_color));
                            }else{
                                subUsageReplaceSpan.setDrawTextColor(mContext.getResources().getColor(R.color.klg_unquality_color));
                            }
                            index++;
                            spannableString.setSpan(subUsageReplaceSpan,0,1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                            spannableStringBuilder.append(spannableString);
                        }
                    }

                }else{
                    spannableStringBuilder.append(sentenceTrainBean.getSentence());
                }
            }
            Log.e("aaaaa","tv_eContent:"+spannableStringBuilder.toString());
            tv_eContent.setText(spannableStringBuilder);
        }
    }

    public void setTrainTimes(int trainTimes){
        this.trainTimes=trainTimes;
        notifyDataSetChanged();
    }

    public void setShowCount(int showCount){
        this.showCount=showCount;
    }

}
